<?php
namespace wl\redis;

require_once __DIR__.'/../../php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
require_once "./RedisService.php";
require_once "./Types.php";

use Thrift\ClassLoader\ThriftClassLoader;

$GEN_DIR = realpath(dirname(__FILE__)).'/../../../gen-php';



$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__ . '/../../php/lib');
//$loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('com', $GEN_DIR);
$loader->register();

if (php_sapi_name() == 'cli') {
    ini_set("display_errors", "stderr");
}

use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TMultiplexedProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;

use wl\redis\Message;

use wl\redis\RedisServiceClient;



class Server{

    public $faceClient=null;
    public $transport=null;
    public function __construct()
    {
        $socket = new TSocket('127.0.0.1', 8848);
        $socket->setSendTimeout(100000);
        $socket->setRecvTimeout(100000);

        $transport = new TBufferedTransport($socket, 1024, 1024);
        $protocol = new TBinaryProtocol($transport);

        $faceProtocol = new TMultiplexedProtocol($protocol, "RedisService");

        $this->faceClient = new RedisServiceClient($faceProtocol);

        $transport->open();
    }

    public function add($score,$value){
        try {

        $message=new Message(["action"=>"zadd","key"=>$score,"value"=>$value]);
        $faceinfo = $this->faceClient->Wlset($message);

//        var_dump(json_decode($faceinfo->action,true));


        } catch (TException $tx) {
            print 'TException: '.$tx->getMessage()."\n";
            print 'TException: '.$tx->getTraceAsString()."\n";
        }
    }


    public function dump(){
        try {

            $message=new Message(["action"=>"dump","key"=>1,"value"=>1]);
            $faceinfo = $this->faceClient->Wlset($message);

            var_dump(json_decode($faceinfo->action,true));


        } catch (TException $tx) {
            print 'TException: '.$tx->getMessage()."\n";
            print 'TException: '.$tx->getTraceAsString()."\n";
        }
    }
    public function del($key){
        $message=new Message(["action"=>"del","key"=>$key,"value"=>0]);
        $faceinfo = $this->faceClient->Wlset($message);

        var_dump(json_decode($faceinfo->action,true));
    }

    public function close(){
                $this->transport->close();
    }

}
